from collections import OrderedDict

from django.utils.datetime_safe import datetime
from requests import Response
from rest_framework.pagination import PageNumberPagination

from utils.common_response import APIResponse


class MyPagination(PageNumberPagination):
    page_size = 20
    page_query_param = 'page'  # /?page=4/  查询第几页的参数
    page_size_query_param = 'size'  # /?page=3&size=4/ 查询第三页，每页显四条参数
    max_page_size = 999  # /最大页数不超过5条

    # def paginate_queryset(self, queryset, request, view=None):
    #     created_at_gte = request.query_params.get('created_at_gte', None)
    #     print(111,created_at_gte)
    #     if created_at_gte:
    #         # 如果存在时间过滤参数，则进行过滤
    #         created_at__gte = request.query_params.get('created_at_gte', None)
    #         created_at__gte = datetime.strptime(created_at__gte, '%Y-%m-%d').date()
    #         # queryset = queryset.filter(created_at__date__gte=created_at__gte)
    #         queryset = queryset.filter(created_at__date=created_at__gte)
    #         print(queryset)
    #     self.count = queryset.count()  # 设置 count 属性
    #     # print(self.count,2313123)
    #
    #     # 模拟结果为空的情况
    #     if queryset.count() == 0:
    #         queryset = []
    #     return super().paginate_queryset(queryset, request, view)
    #
    # def get_paginated_response(self, data):
    #     print(22222)
    #     if not data:  # 当结果为空时
    #         return self.get_empty_paginated_response()  # 返回空字典
    #     return super().get_paginated_response(data)
    #
    # def get_empty_paginated_response(self):
    #     data = {'count': 0,'next': None,'previous': None, 'results': []}
    #     return APIResponse(data)

    def paginate_queryset(self, queryset, request, view=None):
        created_at_gte = request.query_params.get('created_at_gte', None)
        if created_at_gte:
            created_at__gte = datetime.strptime(created_at_gte, '%Y-%m-%d').date()
            queryset = queryset.filter(created_at__date=created_at__gte)

        self.count = queryset.count()
        print(self.count)
        if self.count == 0:  # 如果结果为空
            return None  # 返回 None，表示空结果集

        return super().paginate_queryset(queryset, request, view)

    # def get_paginated_response(self, data):
    #     if not data:
    #         return Response({'count': 0, 'next': None, 'previous': None, 'results': []})
    #     return super().get_paginated_response(data)

